﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Repositories;

namespace Modules.Notify.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class NotifyAttachmentRepository : Repository<NotifyAttachment, DefaultDbContext>
    {
        public NotifyAttachmentRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<NotifyAttachmentRepository> logger)
           : base(dbContext, applicationContext)
        {
        }

        public IEnumerable<NotifyAttachment> GetAttachmentsByNotifyId(string notifyId)
        {
            int areaVersion = EntityCacheService.GetAreaVersion("NotifyId", notifyId);
            return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
            {
                return $"AttachmentsByNotifyId:{notifyId}:{areaVersion}";
            }, () =>
            {
                Sql sql = Sql.Builder
                .Select("id")
                .From("notify_attachment")
                .Where("notify_id=@0  and is_deleted!=1", notifyId);
                sql.OrderBy("create_time DESC");
                return sql;
            });
        }
    }
}